package com.strato.file.remote.v1_0_0.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.osgi.framework.BundleContext;

import com.strato.base.osgi.v1_0_0.BundleUtil;
import com.strato.file.remote.v1_0_0.server.activator.Activator;
import com.strato.file.remote.v1_0_0.server.impl.FileServerImpl;


public interface FileServer {

	public static final String PASSWORD_HEADER="strato_fileserver_password";
	
	public static void createServer(String id,String rootDir,String password){
		FileServer server=new FileServerImpl(id, rootDir,password);
		Map<String, Object> props=new HashMap<String,Object>();
		props.put("id", id);
		BundleContext context = Activator.getContext();
		if(context==null){
			Activator.addToCreateQueue(props,server);
		}else{
			BundleUtil.exportServices(Activator.getContext(), server, new Class<?>[]{FileServer.class}, props);
		}
	}
	
	public static void createServer(String id,String rootDir){
		createServer(id,rootDir,null);
	}
	
	public static FileServer getServer(String id){
		String query="(id="+id+")";
		FileServer server = BundleUtil.getCachedService(Activator.getContext(), FileServer.class, query);
		return server;
	}
	
	static String trimPath(String path){
		StringBuffer buf=new StringBuffer(path);
		while(buf.length()>0&&(buf.charAt(0)=='/'||buf.charAt(0)=='\\')){
			buf.deleteCharAt(0);
		}
		return buf.toString().trim();
	}
	
	void put(String path,InputStream is,String pwd) throws IOException;
	
	void delete(String path,String pwd);
	
	void get(String path,OutputStream os,String pwd) throws IOException;
	
	void copy(String from,String to,String pwd);
	
	boolean exists(String path,String pwd);
}
